﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class UserGroupCtrl
    {
        UserGroupDAO m_UserGroupDAO = new UserGroupDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "UserGroupName";
            cmb.ValueMember = "UserGroupId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "UserGroupName";
            cmb.ValueMember = "UserGroupId";
            cmb.DataPropertyName = "UserGroupId";
            cmb.HeaderText = "Nhóm";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "UserGroupName";
            cmb.ValueMember = "UserGroupId";
            cmb.DataPropertyName = "UserGroupId";
            cmb.HeaderText = "Nhóm";
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtGroupId,
                         TextBoxX txtGroupName,
                         CheckBoxX chkDefault,
                         CheckBoxX chkAdmin,
                         CheckBoxX chkMod,
                         CheckBoxX chkDelete,
                         CheckBoxX chkEdit,
                         CheckBoxX chkReport,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtGroupId.DataBindings.Clear();
            txtGroupId.DataBindings.Add("Text", bs, "UserGroupId");

            txtGroupName.DataBindings.Clear();
            txtGroupName.DataBindings.Add("Text", bs, "UserGroupName");

            chkDefault.DataBindings.Clear();
            chkDefault.DataBindings.Add("Checked", bs, "IsDefault");

            chkAdmin.DataBindings.Clear();
            chkAdmin.DataBindings.Add("Checked", bs, "IsAdmin");

            chkMod.DataBindings.Clear();
            chkMod.DataBindings.Add("Checked", bs, "IsMod");

            chkDelete.DataBindings.Clear();
            chkDelete.DataBindings.Add("Checked", bs, "CanDelete");

            chkReport.DataBindings.Clear();
            chkReport.DataBindings.Add("Checked", bs, "CanReport");

            chkEdit.DataBindings.Clear();
            chkEdit.DataBindings.Add("Checked", bs, "CanEdit");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtGroupId,
                         TextBoxX txtGroupName,
                         CheckBoxX chkDefault,
                         CheckBoxX chkAdmin,
                         CheckBoxX chkMod,
                         CheckBoxX chkDelete,
                         CheckBoxX chkEdit,
                         CheckBoxX chkReport,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtGroupId.DataBindings.Clear();
            txtGroupId.DataBindings.Add("Text", bs, "UserGroupId");

            txtGroupName.DataBindings.Clear();
            txtGroupName.DataBindings.Add("Text", bs, "UserGroupName");

            chkDefault.DataBindings.Clear();
            chkDefault.DataBindings.Add("Checked", bs, "IsDefault");

            chkAdmin.DataBindings.Clear();
            chkAdmin.DataBindings.Add("Checked", bs, "IsAdmin");

            chkMod.DataBindings.Clear();
            chkMod.DataBindings.Add("Checked", bs, "IsMod");

            chkDelete.DataBindings.Clear();
            chkDelete.DataBindings.Add("Checked", bs, "CanDelete");

            chkReport.DataBindings.Clear();
            chkReport.DataBindings.Add("Checked", bs, "CanReport");

            chkEdit.DataBindings.Clear();
            chkEdit.DataBindings.Add("Checked", bs, "CanEdit");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_UserGroupDAO.GetAll();
        }

        public DataTable GetById(String userGroupId)
        {
            return m_UserGroupDAO.GetById(userGroupId);
        }

        public DataTable GetByLikeId(String userGroupId)
        {
            return m_UserGroupDAO.GetByLikeId(userGroupId);
        }

        public DataTable GetByName(String userGroupName)
        {
            return m_UserGroupDAO.GetByName(userGroupName);
        }

        public int Add(UserGroupInfo info)
        {
            return m_UserGroupDAO.Add(info);
        }

        public int Update(UserGroupInfo info)
        {
            return m_UserGroupDAO.Update(info);
        }

        public int Delete(String userGroupId)
        {
            return m_UserGroupDAO.Delete(userGroupId);
        }

        public DataRow AddNewRow()
        {
            return m_UserGroupDAO.AddNewRow();
        }

        public void AddNewGroup(DataRow m_Row)
        {
            m_UserGroupDAO.AddNewGroup(m_Row);
        }

        public bool SaveData()
        {
            return m_UserGroupDAO.SaveData();
        }
    }
}
